package com.monitor.config.interceptor;


import com.alibaba.fastjson.JSONObject;
import com.monitor.commons.NumberUtil;
import com.monitor.commons.SkJsonResult;
import com.monitor.exception.ExceptionMessage;
import com.monitor.web.common.token.AdminToken;
import com.monitor.web.common.token.JwtsUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import java.io.PrintWriter;

/**
 * @author sunke
 * @Date 2018/8/1 01:55
 * @description
 */
@Component(value = "loginInterceptor")
public class LoginInterceptor implements HandlerInterceptor {


    @Autowired
    private JwtsUtil jwtsUtil;

    public static void noLogin(HttpServletResponse response) {
        // response.setContentType("application/json;charset = UTF-8");

//
//        response.addHeader("Access-Control-Allow-Origin", "*");
//        response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD");
//        response.addHeader("Access-Control-Allow-Headers", "Content-Type, Authorization," + JwtsUtil.STAFF_HEADER_KEY + "," + JwtsUtil.STAFF_WPS_HEADER_KEY);
//
//
//        response.addHeader("Access-Control-Max-Age", "3600");


        String msg = "用户未登录";

        try {
            int code = NumberUtil.parseInt(ExceptionMessage.EXIST_CLIENT_NOT_LOGIN.split("@@")[1]);

            response.setContentType("application/json;charset = UTF-8");

            SkJsonResult skJsonResult = new SkJsonResult().setFail(msg);
            skJsonResult.setErrorCode(code);

            PrintWriter writer = response.getWriter();
            writer.write(JSONObject.toJSONString(skJsonResult));
            writer.flush();
            writer.close();


        } catch (Exception e) {

        }

    }

    public JwtsUtil getJwtsUtil() {
        return jwtsUtil;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {


        if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
            response.setStatus(200);
            //  chain.doFilter(request, response);
            return true;
        }


        String header = request.getHeader(JwtsUtil.STAFF_HEADER_KEY);
        if (header == null || !header.startsWith(JwtsUtil.STAFF_HEADER_KEY_PREFIX)) {
            noLogin(response);
            return false;
        }


        try {
            AdminToken adminToken = jwtsUtil.parseAdminToken(header);

            if (adminToken == null) {
                noLogin(response);
                return false;
            }

            //去刷新
            if (jwtsUtil.isRefreshToken(adminToken)) {
                response.setHeader(JwtsUtil.STAFF_HEADER_KEY, jwtsUtil.createAdminToken(adminToken, System.currentTimeMillis()));
            } else {
                response.setHeader(JwtsUtil.STAFF_HEADER_KEY, header);

            }


            return true;

        } catch (Exception e) {

        }


        noLogin(response);
        return false;


    }

}
